package vip.xiaonuo.facedoor.modular.tanscation;

import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Component
public class TranCationCallBackService {

  public void execute(final CallBackAction action) {

    if (TransactionSynchronizationManager.isActualTransactionActive()) {
      TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
        @Override
        public void afterCommit() {
          // 事务提交后执行回调
          action.callback();
        }
      });
    } else {
      // 事务提交后执行回调
      action.callback();
    }

  }

}
